<?php
Box::$description["BoxMosaic"] = "Animated image mosaic";
/**
*Draw a mosaic of images.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage Mosaic
*@todo Add animations.
*/
class BoxMosaic extends Box
{
	private $pictures = array();

	private function GetKey( $row, $col )
	{
		return $row*$this->cols+$col;
	}

	final public function GetPicture( $row, $col )
	{
		$key = $this->GetKey( $row, $col );
		if ( !isset( $this->pictures[$key] ) )
			$this->pictures[$key] = new HtmlEmbeddedPicture( $key, $this);
		return $this->pictures[$key];
	}

	final public function SetPicture( $picture, $row, $col )
	{
		$this->pictures[ $this->GetKey( $row, $col ) ] = $picture;
		$this->SetModified();
		return true;
	}

	public function __construct( $title )
	{
		parent::__construct( $title, "popup" );
		$this->SetStorage(array("piece_width" => "25px",
							"piece_height" => "25px",
							"rows" => 1,
							"cols" => 1 ) );
	}

	public function GenerateHtml()
	{
		$this->AddControl("MosaicConfigure", I18N("Configure"));

		$rows = $this->Get("rows");
		$cols = $this->Get("cols");

		$html = new HtmlTable( $rows, $cols,
												array("class" => "mosaic_table", 
													"style" => new Css(array(
																	"text-align" => "center",
																	"margin-left" => "auto",
																	"margin-right" => "auto"
																))
													) );

		$style = new Css( array("width" => $this->Get("piece_width"),
							"height" => $this->Get("piece_height") ) );

		for ( $r = 0; $r < $rows; $r++ )
		{
			$html->Append( $tr = new HtmlTableRow(1) );
			for ( $c = 0; $c < $cols; $c++ )
			{
				$tr->Append( $td = new HtmlTableCell(1) );
				$picture =  $this->GetPicture($r,$c);
				$td->Append( $picture->GetHtml( array("style" => $style) ) );
			}
		}
		return $html;
	}
}
/**
*Configure the mosaic.
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*@package Boxes
*@subpackage Mosaic
*/
class MosaicConfigure extends BoxEdit
{
	protected function BoxEdit( $box )
	{
		$html = new HtmlFieldset(I18N("Edit mosaic"));

		$html->Append( $form = new HtmlEditRequest() );

		$width = $box->Get("width");
		$form->Append( $fs = new HtmlFieldset( I18N("piece width") ) );
		$fs->Append( new HtmlTextInput("", "width", $width));

		$height = $box->Get("height");
		$form->Append( $fs = new HtmlFieldset( I18N("piece height") ) );
		$fs->Append( new HtmlTextInput( I18N("Height"), "height", $height));

		$rows = $box->Get("rows");
		$cols = $box->Get("cols");
		$form->Append( $fs = new HtmlFieldset( I18N("Rows and columns") ) );
		$fs->Append( new HtmlTextInput("", "rows", $rows ));
		$fs->Append( " X " );
		$fs->Append( new HtmlTextInput("", "cols", $cols ));

		$form->Append( $fs = new HtmlFieldset( I18N("Pictures") ) );
		$fs->Append( $table = new HtmlTable( $rows, $cols ) );

		for ( $r = 0; $r < $rows; $r++ )
		{
			$table->Append( $tr = new HtmlTableRow(1) );
			for ( $c = 0; $c < $cols; $c++ )
			{
				$tr->Append( $td = new HtmlTableCell(1) );
				$picture = $box->GetPicture( $r, $c );
				$td->Append( $picture->Edit() );
			}
		}
		$form->AppendSubmit( I18N("Save!") );
		return $html;
	}
	protected function BoxSave( $box )
	{
		if ( ($width = Core::PostVar("width", false, "string")) !== false )
			$box->Set("width", $width ) && $this->SetReload();
		if ( ($height = Core::PostVar("height", false, "string")) !== false )
			$box->Set("height", $height ) && $this->SetReload();
		if (  ($rows = Core::PostVar("rows", false, "string")) !== false )
			$box->Set("rows",$rows ) && $this->SetReload();
		if (($cols = Core::PostVar("cols", false, "string")) !== false ) 
			$box->Set("cols", $cols ) && $this->SetReload();

		for ( $r = 0; $r < $rows; $r++ )
		{
			for ( $c = 0; $c < $cols; $c++ )
			{
				$picture = $box->GetPicture( $r, $c );
				$picture->Save() && $this->SetReload();
			}
		}
		return I18N("Mosaic configuration saved");
	}
}
?>